package fr.gdi.android.news;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ShareReceiverProxyAdapter extends ArrayAdapter<ShareReceiverProxyAdapter.ShareReceiverProxy>
{
 
    public static final class ShareReceiverProxy 
    {
        String label; 
        String appName; 
        Drawable icon; 
        String receiverName;
        String receiverPackage;
        boolean disabled;
        
        public ShareReceiverProxy(String label, String appName, Drawable drawable, String receiverName, String receiverPackage) 
        {
            this.icon = drawable;
            this.label = label;
            this.appName = appName;
            this.receiverName = receiverName;
            this.receiverPackage = receiverPackage;
        }
        
        public void setDisabled(boolean b) 
        {
            this.disabled = b;
        }
        
        public boolean isDisabled()
        {
            return disabled;
        }
    }
    
    private LayoutInflater inflater;
 
    private boolean showCheckbox;
    
    public ShareReceiverProxyAdapter(Context context, List<ShareReceiverProxyAdapter.ShareReceiverProxy> items)
    {
        super(context, android.R.id.text1, items);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setShowCheckbox(boolean showCheckbox)
    {
        this.showCheckbox = showCheckbox;
    }

    private static class ViewHolder {
        ImageView iconView;
        TextView labelView;
        TextView appNameView;
        CheckBox checkBoxView;
    }

    
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        ViewHolder holder; 
        
        if (v == null) 
        {
            v = inflater.inflate(R.layout.share_receiver_proxy_item, null);
            
            holder = new ViewHolder();
            holder.iconView = (ImageView) v.findViewById(R.id.icon);
            holder.labelView = (TextView) v.findViewById(R.id.label);
            holder.appNameView = (TextView) v.findViewById(R.id.appName);
            holder.checkBoxView = (CheckBox) v.findViewById(R.id.disabled);
            holder.checkBoxView.setVisibility(showCheckbox ? View.VISIBLE : View.GONE);
            
            holder.checkBoxView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean checked)
                {
                    getItem(position).setDisabled(checked);
                }
            });
            
            v.setTag(holder);
        }
        else {
            holder = (ViewHolder)v.getTag();
        }
 
        ShareReceiverProxy item = getItem(position);
        holder.iconView.setImageDrawable(item.icon);
        holder.labelView.setText(item.label);
        holder.appNameView.setText(item.appName);
        if ( showCheckbox ) holder.checkBoxView.setChecked(item.disabled);
        
        return v;
    }
    
}